﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebStudio.Framework.Core;
using System.IO;
using WebStudio.Framework.Extension;
using WebStudio.Framework.Helper;


/// <summary>
/// PageBase 的摘要说明
/// </summary>
public class PageBase : System.Web.UI.Page
{

    //权限

    #region 检查权限方法 [模块权限标识]
    /// <summary>
    /// 检查权限方法 [模块权限标识]
    /// </summary>
    /// <param name="partPowerID">模块权限标识</param>
    /// <returns>是否通过</returns>
    public bool CheckPower(int partPowerID)
    {
        return false;
    }
    #endregion

    #region 检查权限方法 [模块权限标识字符串]
    /// <summary>
    /// 检查权限方法 [模块权限标识字符串]
    /// </summary>
    /// <param name="partPowerIDString">模块权限标识字符串</param>
    /// <returns>是否通过</returns>
    public bool CheckPower(string partPowerIDString)
    {
        return false;
    }
    #endregion


    #region 检查执行权限方法 [模块权限标识]
    /// <summary>
    /// 检查执行权限方法 [模块权限标识]
    /// </summary>
    /// <param name="partPowerID">模块权限标识</param>
    public void CheckExecPower(int partPowerID)
    {
        if (!CheckPower(partPowerID))
        {
            SysVariable.CurrentContext.Response.Clear();
            SysVariable.CurrentContext.Response.Write("对不起，你没有权限执行此操作！");
            SysVariable.CurrentContext.Response.End();
        }
    }
    #endregion

    #region 检查执行权限方法 [模块权限标识字符串]
    /// <summary>
    /// 检查执行权限方法 [模块权限标识字符串]
    /// </summary>
    /// <param name="partPowerIDString">模块权限标识字符串</param>
    public void CheckExecPower(string partPowerIDString)
    {
        if (!CheckPower(partPowerIDString))
        {
            SysVariable.CurrentContext.Response.Clear();
            SysVariable.CurrentContext.Response.Write("对不起，你没有权限执行此操作！");
            SysVariable.CurrentContext.Response.End();
        }
    }
    #endregion


    #region 系统退出方法
    /// <summary>
    /// 系统退出方法
    /// </summary>
    public void SystemExit()
    {
        //TODO: Exit system
    }
    #endregion


     

    //公共属性

    //#region 当前用户属性
    ///// <summary>
    ///// 当前用户属性
    ///// </summary>
    //public UserInfo CurrentUser
    //{
    //    get
    //    {
    //        return UserAppI.CurrentUser;
    //    }
    //}
    //#endregion
      
    #region 命令代码属性
    /// <summary>
    /// 命令代码属性
    /// </summary>
    public string CommandCode
    {
        get
        {
            return ConvertHelper.StringParse(Request["CommandCode"]);
        }
    }
    #endregion

    #region 页面大小属性
    /// <summary>
    /// 页面大小属性
    /// </summary>
    public int PageSize
    {
        get
        {
            if (Request.Cookies["PageSize"] == null)
            {
                Response.Cookies["PageSize"].Value = "50";
                Response.Cookies["PageSize"].Expires = DateTime.MaxValue;
                return 50;
            }
            else if (ViewState["PageSize"] != null)
            {
                return ConvertHelper.IntParse(ViewState["PageSize"]);
            }
            else
            {
                return ConvertHelper.IntParse(Request.Cookies["PageSize"].Value);
            }
        }
        set
        {
            ViewState["PageSize"] = value.ToString();
            Response.Cookies["PageSize"].Value = value.ToString();
            Response.Cookies["PageSize"].Expires = DateTime.MaxValue;
        }
    }
    #endregion

    #region 页面索引属性
    /// <summary>
    /// 页面索引属性
    /// </summary>
    public int PageIndex
    {
        get
        {
            if (ViewState["PageIndex"] != null)
            {
                return ConvertHelper.IntParse(ViewState["PageIndex"]);
            }
            else
            {
                return ConvertHelper.IntParse(Request["PageIndex"]);
            }
        }
        set
        {
            ViewState["PageIndex"] = value;
        }
    }
    #endregion

    #region 检索条件属性
    /// <summary>
    /// 检索条件属性
    /// </summary>
    public string SearchCondition
    {
        get
        {
            if (ViewState["SearchCondition"] != null)
            {
                return ConvertHelper.StringParse(ViewState["SearchCondition"]);
            }
            else if (!string.IsNullOrEmpty(Request["SearchCondition"]))
            {
                return ConvertHelper.StringParse(Request["SearchCondition"]).HttpBase64ToBase64().EncryptSymmetric();
            }
            else
            {
                return "";
            }
        }
        set
        {
            ViewState["SearchCondition"] = value;
        }
    }
    #endregion

    #region 排序表达式属性
    /// <summary>
    /// 排序表达式属性
    /// </summary>
    public string SortExpression
    {
        get
        {
            if (ViewState["SortExpression"] != null)
            {
                return ConvertHelper.StringParse(ViewState["SortExpression"]);
            }
            else
            {
                return ConvertHelper.StringParse(Request["SortExpression"]);
            }
        }
        set
        {
            ViewState["SortExpression"] = value;
        }
    }
    #endregion

    #region 查询字符串属性
    /// <summary>
    /// 查询字符串属性
    /// </summary>
    public string QueryString
    {
        get
        {
            //初始化
            string _QueryString = "";
            _QueryString += "&PageIndex=" + PageIndex.ToString();
            _QueryString += "&SearchCondition=" + SearchCondition.Base64ToHttpBase64();
            _QueryString += "&SortExpression=" + SortExpression;

            //返回
            return _QueryString;
        }
    }
    #endregion

    #region 主题路径属性
    /// <summary>
    /// 主题路径属性
    /// </summary>
    public string ThemesPath
    {
        get
        {
            return "../../App_Themes/" + ApplicationConfiguration.ThemeCode;
        }
    }
    #endregion


    //脚本方法

    #region 执行客户端脚本函数 [RegisterClientScriptBlock]
    /// <summary>
    /// 执行客户端脚本函数 [RegisterClientScriptBlock]
    /// </summary>
    /// <param name="scriptBody">脚本体</param>
    protected void ExcuteScript(string scriptBody)
    {
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "script", scriptBody, true);
    }
    #endregion

    #region 执行客户端脚本函数 [RegisterStartupScript]
    /// <summary>
    /// 执行客户端脚本函数 [RegisterStartupScript]
    /// </summary>
    /// <param name="scriptBody">脚本体</param>
    protected void ExcuteStartupScript(string scriptBody)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "script", scriptBody, true);
    }
    #endregion


    //公共事件

    #region 预初始化事件 [PreInit]
    /// <summary>
    /// 预初始化事件 [PreInit]
    /// </summary>
    /// <param name="e">事件对象</param>
    protected override void OnPreInit(EventArgs e)
    {
        //主题代码初始化
        string themeCode = ApplicationConfiguration.ThemeCode;

        //加载MASTER [MasterPageFile="~/App_Master/Default/BodyFrame.master"]
        if (Page.MasterPageFile != null)
        {
            //初始化
            string fileName = Page.MasterPageFile.Substring(Page.MasterPageFile.LastIndexOf('/'));
            string path = Page.MasterPageFile.Substring(0, Page.MasterPageFile.LastIndexOf('/'));
            path = path.Substring(0, path.LastIndexOf('/'));
            string masterFile = path + "/" + themeCode + "/" + fileName;

            //加载布局
            if (File.Exists(Server.MapPath(masterFile)))
            {
                Page.MasterPageFile = masterFile;
            }
        }

        //加载主题
        if (!string.IsNullOrEmpty(Page.Theme))
        {
            Page.Theme = themeCode;
        }

        //重载
        base.OnPreInit(e);
    }
    #endregion

    #region 页面加载事件 [OnLoad]
    /// <summary>
    /// 页面加载事件 [OnLoad]
    /// </summary>
    /// <param name="e">事件参数</param>
    protected override void OnLoad(EventArgs e)
    {
        //第一次加载页面
        if (!IsPostBack)
        {
            //激活已登陆用户
            if (Request.IsAuthenticated)
            {
              
            }
        }

        //加载基类事件
        base.OnLoad(e);
    }
    #endregion

}